package leedcode_path.Link;

public class 删除链表的倒数第N个结点 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummynode=new ListNode(0,head);
        int num=0;
        ListNode cur=dummynode;
        while (cur.next!=null){
            cur=cur.next;
            num++;
        }
        int flag=num-n+1;
        cur=dummynode;
        while (flag!=1){
            cur=cur.next;
            flag--;
        }
        cur.next=cur.next.next;
        return dummynode.next;
    }

    public ListNode removeNthFromEnd1(ListNode head, int n) {
        ListNode dummynode=new ListNode(0,head);
        ListNode slow=dummynode;
        ListNode fast=dummynode;
        for (int i = 0; i < n; i++) {
            fast=fast.next;
        }
        while (fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        slow.next=slow.next.next;
        return dummynode.next;
    }
}
